import { Component } from '@angular/core';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.less']
})
export class AppComponent {
  title = 'MQTTClient';

  constructor() {

  }

  connect() {

    let client = window['mqtt']('ws://localhost:5001/mqtt', {


    });

    client.subscribe('server/test/mqttjs', function (err, granted) {
      
      console.log(err, granted)
    })

    console.log(client)
    client.on('connect', function (e) {
      console.log('connect')
      client.subscribe('presence', function (err) {
        if (!err) {
          client.publish('client/test/mqttjs', 'Hello mqtt');
        }
      })
    })

    client.on('message', function (topic, message) {
      // message is Buffer
      console.log('服务端连接成功')
      console.log(topic, message.toString())
      client.end()
    })

    client.on('error', function (error) {
      console.log(error)
    }).on('end', function () {
      console.log('mqtt connect end.')
    }).on('offline', function () {
      console.log('mqtt connect offline.')
    })

  }

}
